<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="50px">
      <el-form-item label="姓名" prop="employeeName">
        <el-input
          v-model="queryParams.employeeName"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工号" prop="jobNo">
        <el-input
          v-model="queryParams.jobNo"
          placeholder="请输入工号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="年月" prop="ny">
        <el-date-picker
          v-model="queryParams.ny"
          type="month"
          placeholder="选择年月">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['hr:gsjs:edit']"-->
<!--        >计算</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['hr:gsjs:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['hr:gsjs:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
        >导入</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="gsjsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="年月" align="center" prop="ny" width="120"/>
      <el-table-column label="姓名" align="center" prop="employeeName" />
      <el-table-column label="工号" align="center" prop="jobNo" width="140"/>
      <el-table-column label="应发工资" align="center" prop="yfgz" />
      <el-table-column width="120" label="扣除数金额" align="center" prop="kcshu" />
      <el-table-column width="120" label="社保公积金金额" align="center" prop="sbgjj" />
      <el-table-column width="120" label="累计工资金额" align="center" prop="ljgz" />
      <el-table-column width="120" label="累计扣除数金额" align="center" prop="ljkcs" />
      <el-table-column label="累计社保" align="center" prop="ljsb" />
      <el-table-column label="子女教育" align="center" prop="znjy" />
      <el-table-column label="赡养老人" align="center" prop="sylr" />
      <el-table-column width="120" label="住房贷款利息" align="center" prop="zfdklx" />
      <el-table-column label="住房租金" align="center" prop="zfzj" />
      <el-table-column label="继续教育" align="center" prop="jxjy" />
      <el-table-column width="120" label="应纳税所得额" align="center" prop="ynssde" />
      <el-table-column label="税率" align="center" prop="rate" />
      <el-table-column width="120" label="速算扣除数" align="center" prop="sskcs" />
      <el-table-column label="应纳税额" align="center" prop="ynse" />
      <el-table-column label="已交税额" align="center" prop="yjse" />
      <el-table-column label="实缴税额" align="center" prop="sjse" />
      <el-table-column label="备注" align="center" prop="remark" />
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="120">-->
<!--        <template slot-scope="scope">-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['hr:gsjs:edit']"-->
<!--          >计算</el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['hr:gsjs:remove']"-->
<!--          >删除</el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改个税计算对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="年月" prop="ny" >
              <el-date-picker clearable
                v-model="form.ny"
                type="month"
                value-format="yyyy-MM"
                              readonly
                placeholder="请选择年月">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="工号" prop="jobNo">
              <el-input v-model="form.jobNo" placeholder="请选择员工" readonly>
<!--                <el-button slot="append" icon="el-icon-search" @click="handleSelectPerson"></el-button>-->
              </el-input>
            </el-form-item>
<!--            <PersonSelect ref="personSelect" @onSelected="onPersonSelected" ></PersonSelect>-->
          </el-col>
          <el-col :span="8">
            <el-form-item label="姓名" prop="employeeName">
              <el-input v-model="form.employeeName" placeholder="" readonly/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="应发工资" prop="yfgz">
              <el-input v-model="form.yfgz" placeholder="请输入应发工资" readonly/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="扣除数金额" prop="kcshu">
              <el-input v-model="form.kcshu" placeholder="请输入扣除数金额" readonly/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="社保公积金金额" prop="sbgjj">
              <el-input v-model="form.sbgjj" placeholder="请输入社保公积金金额" readonly/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="累计工资金额" prop="ljgz">
              <el-input v-model="form.ljgz" placeholder="请输入累计工资金额"readonly />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="累计扣除数金额" prop="ljkcs">
              <el-input v-model="form.ljkcs" placeholder="请输入累计扣除数金额" readonly/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="累计社保" prop="ljsb">
              <el-input v-model="form.ljsb" placeholder="请输入累计社保" readonly/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="子女教育" prop="znjy" style="color: #ea4141">
              <el-input v-model="form.znjy" placeholder="请输入子女教育" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="赡养老人" prop="sylr">
              <el-input v-model="form.sylr" placeholder="请输入赡养老人" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="住房贷款利息" prop="zfdklx">
              <el-input v-model="form.zfdklx" placeholder="请输入住房贷款利息" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="住房租金" prop="zfzj">
              <el-input v-model="form.zfzj" placeholder="请输入住房租金" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="继续教育" prop="jxjy">
              <el-input v-model="form.jxjy" placeholder="请输入继续教育" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="应纳税所得额" prop="ynssde">
              <el-input v-model="form.ynssde" placeholder="请输入应纳税所得额" readonly/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="税率" prop="rate">
              <el-input v-model="form.rate" placeholder="请输入税率" readonly/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="速算扣除数" prop="sskcs">
              <el-input v-model="form.sskcs" placeholder="请输入速算扣除数" readonly/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="应纳税额" prop="ynse">
              <el-input v-model="form.ynse" placeholder="请输入应纳税额" readonly/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="已交税额" prop="yjse">
              <el-input v-model="form.yjse" placeholder="请输入已交税额" readonly/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="实缴税额" prop="sjse">
              <el-input v-model="form.sjse" placeholder="请输入实缴税额" readonly/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">计算并回写</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :close-on-click-modal="false" :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <!--  <div class="el-upload__tip" slot="tip">
              <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的用户数据
            </div>-->
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listGsjs, getGsjs, delGsjs, addGsjs, updateGsjs } from "@/api/hr/gsjs";
import {getToken} from "@/utils/auth";
import PersonSelect from "@/components/personSelect";
import moment from "moment/moment";

export default {
  name: "Gsjs",
  components :{PersonSelect},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 个税计算表格数据
      gsjsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        gcCode: null,
        gcName: null,
        ny: null,
        employeeName: null,
        jobNo: null,
        yfgz: null,
        kcshu: null,
        sbgjj: null,
        ljgz: null,
        ljkcs: null,
        ljsb: null,
        znjy: null,
        sylr: null,
        zfdklx: null,
        zfzj: null,
        jxjy: null,
        ynssde: null,
        rate: null,
        sskcs: null,
        ynse: null,
        yjse: null,
        sjse: null,
      },
      // 表单参数
      form: {},
      //考勤导入
      upload: {
        // 是否显示弹出层（物料导入）
        open: false,
        // 弹出层标题（物料导入）
        title: "导入原始考勤",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/hr/gsjs/importData"
      },
      // 表单校验
      rules: {
        znjy: [
          { required: true, trigger: "blur", message: "请输入子女教育金" }
        ],
        sylr: [
          { required: true, trigger: "blur", message: "请输入赡养老人金" }
        ],
        zfdklx: [
          { required: true, trigger: "blur", message: "请输入住房贷款利息" }
        ],
        zfzj: [
          { required: true, trigger: "blur", message: "请输入住房租金" }
        ],
        jxjy: [
          { required: true, trigger: "blur", message: "请输入继续教育金" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  activated(){
    if(this.$route.query.jobNo){
      console.log(this.$route.query.jobNo,"this.$route.query.jobNo")
      this.queryParams.jobNo = this.$route.query.jobNo
      this.queryParams.ny = this.$route.query.ny
    }
    this.getList();
  },
  methods: {
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "数据导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('hr/gsjs/importTemplate', {
      }, `个税计算.xlsx`)
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    /** 查询个税计算列表 */
    getList() {
      this.loading = true;
      listGsjs(this.queryParams).then(response => {
        this.gsjsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        gcCode: null,
        gcName: null,
        ny: null,
        employeeName: null,
        jobNo: null,
        yfgz: null,
        kcshu: null,
        sbgjj: null,
        ljgz: null,
        ljkcs: null,
        ljsb: null,
        znjy: null,
        sylr: null,
        zfdklx: null,
        zfzj: null,
        jxjy: null,
        ynssde: null,
        rate: null,
        sskcs: null,
        ynse: null,
        yjse: null,
        sjse: null,
        remark: null,
        createTime: null,
        createBy: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加个税计算";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getGsjs(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "个税计算";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.form.ny = moment.utc(this.form.ny).local().format('YYYY-MM')
      console.log(this.form,"this.form")
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateGsjs(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
            this.$router.push('/hrsystem/xinzhouguanli/gzgl/gzhs');
          } else {
            addGsjs(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除个税计算编号为"' + ids + '"的数据项？').then(function() {
        return delGsjs(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('hr/gsjs/export', {
        ...this.queryParams
      }, `gsjs_${new Date().getTime()}.xlsx`)
    },
    handleSelectPerson(){
      this.$refs.personSelect.showFlag = true;
    },
    onPersonSelected(obj){
      if(obj != undefined && obj != null){
        this.form.jobNo = obj.jobNo;
        this.form.employeeName = obj.pname;
      }
    },
  }
};
</script>
